package com.jastar.android.util;

import android.content.Context;

import com.jastar.android.game.richman.logic.handler.BaseCallback;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class TimeLocker {

    private static final Map<String, Integer> maxMap = new HashMap<>();
    private static final Map<String, Integer> lastSecondMap = new HashMap<>();
    private static final Map<String, Integer> countMap = new HashMap<>();

    public static void lock(Context context, String lockId, int max, BaseCallback callback) {
        if (!maxMap.containsKey(lockId)) {
            maxMap.put(lockId, max);
            lastSecondMap.put(lockId, 0);
            countMap.put(lockId, 0);
        }
        int count = countMap.get(lockId);
        int lastSecond = lastSecondMap.get(lockId);

        int s = Calendar.getInstance().get(Calendar.SECOND);
        if (s - lastSecond == 0) return;
        Alert.toast(context, String.valueOf(s));

        if (count == 0) {
            countMap.put(lockId, 1);
        } else if (s - lastSecond == 3 || s - lastSecond == 4) {
            countMap.put(lockId, count + 1);
        } else {
            countMap.put(lockId, 0);
        }
        if (countMap.get(lockId) == 3) {
            callback.onResult();
        }
        lastSecondMap.put(lockId, s);
    }

}
